{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 264,
   "id": "f2c3496f-5fc3-4975-a812-f5a066dda84f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import json\n",
    "import glob\n",
    "import shutil\n",
    "\n",
    "import numpy as np\n",
    "import os.path as osp\n",
    "import fiftyone as fo\n",
    "\n",
    "from tqdm import tqdm\n",
    "from pycocotools.coco import COCO\n",
    "from pycocotools import mask as maskUtils\n",
    "\n",
    "from my_class import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "id": "1769f529-4f05-43b8-9ddc-37edc4367ff0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nums of batch:  100\n"
     ]
    }
   ],
   "source": [
    "metric_num = \"zsceshi0415\"\n",
    "root_dir = f\"/home/user/workspace/results/{metric_num}/\"\n",
    "batch_nums = os.listdir(root_dir)\n",
    "\n",
    "result_dir_images = '/home/tmy/1-Nike数据整理/nike_0416/leak'\n",
    "            # os.makedirs(dist, exist_ok=True)\n",
    "\n",
    "# 取前5个批次\n",
    "batch_nums = batch_nums[:100]\n",
    "print(\"nums of batch: \", len(batch_nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "id": "f9cc56cd-792a-4e18-906c-3a1a7fbe3064",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 100/100 [06:08<00:00,  3.68s/it]\n"
     ]
    }
   ],
   "source": [
    "def parse_result(annFile):\n",
    "    try:\n",
    "        with open(annFile, \"r\") as f:\n",
    "            data = json.load(f)[0]\n",
    "        predictions = data[\"predictions\"]\n",
    "        fg = data['offset'][:4]\n",
    "        result = [Anomaly(data['offset'][4], fg, None, \"shoe\")]\n",
    "        \n",
    "    \n",
    "        for item in predictions:\n",
    "            flaw = Anomaly(item[\"scores\"], item[\"bboxes\"], item[\"segmentation\"], item[\"category_name\"])\n",
    "            result.append(flaw)\n",
    "        return result\n",
    "    except Exception as e:\n",
    "        return None\n",
    "\n",
    "samples = []\n",
    "\n",
    "for batch in tqdm(batch_nums):\n",
    "    img_file = glob.glob(f\"{osp.join(root_dir, batch)}/**/*white.png\", recursive=True)\n",
    "    img_shape = (4096, 3000)\n",
    "    img_w = img_shape[0]\n",
    "    img_h = img_shape[1]\n",
    "    \n",
    "    for img in img_file:\n",
    "        pred_file = img.replace(\".png\", \".json\")\n",
    "        flaws = parse_result(pred_file)\n",
    "        detections = []\n",
    "        segmentations = []\n",
    "        if flaws is None:\n",
    "            continue\n",
    "\n",
    "        if len(flaws) == 1:\n",
    "            shutil.copy2(img, result_dir_images)   \n",
    "\n",
    "        for item in flaws:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "id": "c5089275-e844-44f2-b6cb-831ba08403d0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5391\n"
     ]
    }
   ],
   "source": [
    "print(len(leak))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b88d9410-cd9a-4b97-bce5-3a24ea3e9e91",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
